//
//  TodayEarningsViewController.m
//  NewPro
//
//  Created by RYX on 2019/1/7.
//  Copyright © 2019 何荧红. All rights reserved.
//

#import "TodayEarningsViewController.h"
#import "TodayEarningsCell.h"
#import "TodayEarningsTableHeaderView.h"
#import "TodayEarningsStore.h"
#import "TodayEarningsModel.h"
#import "BackTotalModel.h"
#import "IncomeListModel.h"
@interface TodayEarningsViewController ()<UITableViewDelegate,UITableViewDataSource>
{
    
    __weak IBOutlet UILabel *currentTimeLa;
    
    NSString *parameterDate;
}

@property (nonatomic,strong) UIView *headerView;
@property (nonatomic,strong) TodayEarningsTableHeaderView *tableHeaderView;
@property (nonatomic,copy) NSArray *backTotalArr;
@property (nonatomic,copy) NSArray *incomeListArr;
@property (nonatomic, copy) NSString *seletime;

@end

@implementation TodayEarningsViewController

- (NSArray *)backTotalArr{
    
    if (!_backTotalArr) {
        
        _backTotalArr = [NSArray array];
    }
    
    return _backTotalArr;
}

- (NSArray *)incomeListArr{
    
    if (!_incomeListArr) {
        
        _incomeListArr = [NSArray array];
    }
    
    return _incomeListArr;
}

- (UITableView *)tableView{
    
    if (!_tableView) {
        
        _tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStyleGrouped];
        _tableView.backgroundColor = [UIColor clearColor];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.sectionFooterHeight = 4.5;
        _tableView.sectionHeaderHeight = 0.5;
        _tableView.tableHeaderView = self.tableHeaderView;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        
        __weak typeof(self) weakSelf = self;
        MJRefreshNormalHeader *header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
           
            [weakSelf loadData];
        }];
        
        header.lastUpdatedTimeLabel.hidden = YES;
        
        _tableView.mj_header = header;
        
        [_tableView.mj_header beginRefreshing];
    }
    
    return _tableView;
}


- (UIView *)headerView{
    
    if (!_headerView) {
        
//        _headerView = [[[NSBundle mainBundle] loadNibNamed:@"TodayEarningsHeaderView" owner:self options:nil] objectAtIndex:0];
        _headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, 45)];
        
    }
    
    return _headerView;
}

- (TodayEarningsTableHeaderView *)tableHeaderView{
    
    if (!_tableHeaderView) {
        
        _tableHeaderView = [[TodayEarningsTableHeaderView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, 150)];

    }
    
    return _tableHeaderView;
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:YES];
    self.navigationController.navigationBar.hidden = YES;
}
- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:YES];
    self.navigationController.navigationBar.hidden = NO;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    NSDate *date=[NSDate date];
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
    dateFormatter.dateFormat = @"yyyy-MM-dd";
    parameterDate = [dateFormatter stringFromDate:date];
    
    ViewHeader *header = [[ViewHeader alloc]initWithDefaultString:@"当日收益详情"];
    [self.view addSubview:header];
    
    [self.view addSubview:self.headerView];
    
    [self.view addSubview:self.tableView];
    
    NSDateFormatter *formart = [[NSDateFormatter alloc] init];
    [formart setDateFormat:@"yyyy年MM月dd日"];
    currentTimeLa.text = [formart stringFromDate:date];
    
    [self.headerView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.height.mas_equalTo(45);
        make.top.equalTo(header.mas_bottom);
        make.left.right.equalTo(self.view).offset(0);
        
    }];
    
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {

        make.top.equalTo(self.headerView.mas_bottom).offset(2);
        make.left.right.bottom.equalTo(self.view);

    }];
    
}

- (IBAction)onCalendarBtn:(UIButton *)sender {
    
    NSDate *minDate = [NSDate setYear:1978 month:3 day:12];
    NSDate *maxDate = [NSDate date];
    
    [BRDatePickerView showDatePickerWithTitle:@"选择日期" dateType:BRDatePickerModeYMD defaultSelValue:self.seletime minDate:minDate maxDate:maxDate isAutoSelect:YES themeColor:nil resultBlock:^(NSString *selectValue) {
        self.seletime = selectValue;
        self->parameterDate = selectValue;
        
        NSArray *listArr = [selectValue componentsSeparatedByString:@"-"];
        self->currentTimeLa.text = [NSString stringWithFormat:@"%@年%@月%@日",listArr[0],listArr[1],listArr[2]];
        
    } cancelBlock:^{
        
        
    }];
}

#pragma mark - UITableViewDelegate,UITableViewDataSource
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return [TodayEarningsCell getHeightWithIndexPath:indexPath];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    if (section == 0) {
        
        return self.backTotalArr.count;
    }
    
    return self.incomeListArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    TodayEarningsCell *cell = [TodayEarningsCell tempWithTableView:tableView andIndexPath:indexPath];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    if (indexPath.section == 0) {
        
        cell.model1 = self.backTotalArr[indexPath.row];
    }else{
        
        cell.model2 = self.incomeListArr[indexPath.row];
    }
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
}

-(void)viewDidLayoutSubviews {
    
    if ([self.tableView respondsToSelector:@selector(setSeparatorInset:)]) {
        [self.tableView setSeparatorInset:UIEdgeInsetsZero];
        
    }
    if ([self.tableView respondsToSelector:@selector(setLayoutMargins:)])  {
        [self.tableView setLayoutMargins:UIEdgeInsetsZero];
    }
}

#pragma mark - 查询
- (IBAction)onSearchBtn:(UIButton *)sender {
    
    if (currentTimeLa.text.length <= 0) {
        
        return;
    }
    
    [self.tableView.mj_header beginRefreshing];
}

#pragma mark - 加载数据
- (void)loadData{
    
    MJWeakSelf
    TodayEarningsStore *store = [[TodayEarningsStore alloc] init];
    [store getTodayEarningsWithCustomId:[UserDefaultsManager sharedInstance].user_id andDate:parameterDate Success:^(TodayEarningsModel *model) {
        
        [weakSelf.tableView.mj_header endRefreshing];
        
        weakSelf.tableHeaderView.model = model;
        
//        weakSelf.backTotalArr = [BackTotalModel mj_objectArrayWithKeyValuesArray:model.backTotal];
//        weakSelf.incomeListArr = [IncomeListModel mj_objectArrayWithKeyValuesArray:model.incomeList];
        
        [weakSelf.tableView reloadData];
        
    } Failure:^(NSError *error) {
        
        [weakSelf.tableView.mj_header endRefreshing];
        [weakSelf showMBPError:[HttpTool handleError:error]];
    }];
    
}
@end
